<?php

namespace App\Models\Entity\Column;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * Class Collist
 * @package App\Models\Entity\Column
 * @version August 1, 2017, 4:32 pm UTC
 */
class Collist extends Model
{
    use SoftDeletes;

    public $table = 'collists';

    protected $dates = ['deleted_at'];

    public $fillable = [
        // 'collist_no',
        'name',
        'title',
        'subtitle',
        'short_desc',
        'long_desc',
        'img',
        'created_at',
        'updated_at',
        'listorder',
        // 上下线, 发布
        'online_status',
        'moderate_status',
        'publish_status',
        'moderate_by',
        'moderate_at',
        'online_at',
        'offline_at',
        'user_id',
        'publish_down_status',
        'sp_id',
        'data',
        // 设备类型ID
        'term_type_id',
        'level',
        // 模板样式发哪位
        'range',
        // 模板ID
        'tpl_id',
        // 背景图
        'bg',
        // onclick信息描述
        'message',

    ];

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        // 'collist_no' => 'string',
        'name'       => 'string',
        'title'      => 'string',
        'subtitle'   => 'string',
        'short_desc' => 'string',
        'long_desc'  => 'string',
        'img'        => 'string',
        'listorder'  => 'integer',
    ];

    /**
     * Validation rules
     *
     * @var array
     */
    public static $rules = [
        // 'collist_no' => 'required|max:100|unique:collists,collist_no',
        'name'       => 'required|max:100',
        'title'      => 'max:100',
        'subtitle'   => 'max:100',
        'short_desc' => 'required|max:100',
        'img'        => 'max:200',
    ];

    public function cols()
    {
        return $this->belongsToMany(\App\Models\Entity\Column\Column::class, 'collist_columns', 'collist_id', 'column_id');
    }
    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     **/
    public function sp()
    {
        return $this->hasOne(\App\Models\Common\EntityData\Channel\Sp::class, 'id', 'sp_id');
    }

    /**
     * 栏目列表数据
     * @return [type] [description]
     */
    public function collistDatas()
    {
        return $this->hasMany(\App\Models\Application\CollistData::class, 'collist_id', 'id');

    }
    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     **/
    public function termType()
    {
        return $this->hasOne(\App\Models\Application\TermType::class, 'id', 'term_type_id');
    }

    /**
     * 获得此媒资的所有标签。
     */
    public function tags()
    {
        return $this->morphToMany(\App\Models\Entity\Tag\tag::class, 'tag_content');
    }
    /**
     * 过滤上下线数据
     **/
    public function scopeOnlineStatus($query, $status = 1)
    {
        return $query->where('online_status', $status);
    }

    /**
     * 过滤上线发布数据
     **/
    public function scopeOnlinePublished($query, $status = 1)
    {
        return $query->where(['online_status' => 1, 'publish_status' => $status]);
    }

    /**
     * 过滤下线发布数据
     **/
    public function scopeOfflinePublished($query, $status = 1)
    {
        return $query->where(['online_status' => 0, 'publish_down_status' => $status]);
    }
}
